package puf.m2.automaton;

import java.io.Serializable;

public class State implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name;
    /** The state id, used for generating */
    private int id;
    /** The automaton this state belongs to. */
    private Automaton automaton = null;

    public State(int id, Automaton automaton) {
        this.id = id;
        this.automaton = automaton;
        this.name = name();
    }

    /**
     * Returns a string representation of this object.
     */
    public String toString() {
        return "q_" + Integer.toString(id);
    }

    /**
     * Returns the state ID for this state.
     * @return the ID of the state
     */
    public int id() {
        return id;
    }

    /**
     * Sets the ID for this state.
     * @param id
     *      the new ID for this state.
     */
    protected void setID(int id) {
        if (("q" + this.id).equals(name))
            name = null;
        this.id = id;
    }

    /**
     * Sets the name for this state. A parameter of <CODE>null</CODE> will
     * reset this to the default.
     * @param name
     *      the new name for the state
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the simple "name" for this state. By default this will simply be
     * "qd", where d is the ID number.
     *
     * @return the name for this state
     */
    public String name() {
        if (name == null) {
            name = "q" + Integer.toString(id);
        }
        return name;
    }

    public void setAutomaton(Automaton automaton) {
        this.automaton = automaton;
    }

    /**
     * Returns the automaton that this state belongs to.
     */
    public Automaton getAutomaton() {
        return automaton;
    }
}
